package com.china08.yxyapi.api.manage.tec.office.attendance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.attendance.YxyYxyTecAttendanceConfig;
import com.china08.yxyapi.entity.db.yxy.YxyHoliday;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.attendance.YxyTecAttendanceConfigRepository;
import com.china08.yxyapi.repository.db.yxy.YxyHolidayRepository;
import com.china08.yxyapi.util.DateUtils;

/**
 * 教师考勤-我的考勤-今日考勤-本周是否需要打卡
 * 
 * @author LiuWeidong
 *
 */

@CrossOrigin
@RestController
public class AttendanceYxyHolidaySrhController extends YxyApiBaseController {

    @Autowired
    private YxyHolidayRepository yxyHolidayRepostitory;

    @Autowired
    private YxyTecAttendanceConfigRepository yxyTecAttendanceConfigRepository;

    @RequestMapping(value = "/api_yw/manage/tec/office/attendance/yxyHolidaySrh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(//
            HttpServletRequest request, //
            HttpServletResponse response) {
        String teacherId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        RespModel respModel = new RespModel();
        List<ThisAttendance> thisWeekHolis = new ArrayList<>();
        YxyYxyTecAttendanceConfig config = yxyTecAttendanceConfigRepository.findBySchoolId(schoolId);
        Assert.isTrue(null != config, "管理员未设置打卡时间！");

        // 1、先取得学校配置信息，星期 1-7，哪天是正常上班。
        // 2、取得本周的日期（共7天）
        // 3、将 学校配置中的星期+寒暑假和holiday表中的日期进行叠加，计算出是否假日。
        // 4、拿到本周日期
        for (int i = 1; i <= 7; i++) {
            // 周几的日期
            Date thisWeekDate = DateUtils.getThisWeek(new Date(), i);
            YxyHoliday yxyHoliday = yxyHolidayRepostitory.findByDay(DateUtils.formatDate2(thisWeekDate));
            ThisAttendance thisWeekAttendance = new ThisAttendance();
            thisWeekAttendance.week = this.getWeek(i);
            if (yxyHoliday != null) {
                thisWeekAttendance.isHoliday = true;
                thisWeekAttendance.description = yxyHoliday.getDescription();
                thisWeekHolis.add(thisWeekAttendance);
                continue;
            } else if (betweenDate(thisWeekDate, config.getWinterVacationBegin(), config.getWinterVacationEnd())) {
                thisWeekAttendance.isHoliday = true;
                thisWeekAttendance.description = "寒假";
                thisWeekHolis.add(thisWeekAttendance);
                continue;

            } else if (betweenDate(thisWeekDate, config.getSummerVacationBegin(), config.getSummerVacationEnd())) {
                thisWeekAttendance.isHoliday = true;
                thisWeekAttendance.description = "暑假";
                thisWeekHolis.add(thisWeekAttendance);
                continue;
            } else {
                if (weekend(config, i)) {
                    thisWeekAttendance.isHoliday = false;
                    thisWeekAttendance.description = "打卡";
                    thisWeekHolis.add(thisWeekAttendance);
                } else {
                    thisWeekAttendance.isHoliday = true;
                    thisWeekAttendance.description = "放假";
                    thisWeekHolis.add(thisWeekAttendance);
                }
            }

        }
        respModel.schoolId = schoolId;
        respModel.teacherId = teacherId;
        respModel.thisWeekHolis = thisWeekHolis;
        return respModel;
    }

    private boolean betweenDate(Date thisDate, Date beginDate, Date endDate) {

        // if (thisDate.getTime() >= beginDate.getTime() && thisDate.getTime()
        // <= endDate.getTime()) { return true; }

        if (null != beginDate && null != endDate) {
            if (DateUtils.formatDate4(thisDate) >= DateUtils.formatDate4(beginDate)
                    && DateUtils.formatDate4(thisDate) <= DateUtils.formatDate4(endDate)) { return true; }
        }
        return false;
    }

    private boolean weekend(YxyYxyTecAttendanceConfig config, int i) {
        if (i == 1) { return config.getMonday(); }
        if (i == 2) { return config.getTuesday(); }
        if (i == 3) { return config.getWednesday(); }
        if (i == 4) { return config.getThursday(); }
        if (i == 5) { return config.getFriday(); }
        if (i == 6) { return config.getSaturday(); }
        if (i == 7) { return config.getSunday(); }
        return false;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {
        public List<ThisAttendance> thisWeekHolis;
        public String teacherId;
        public String schoolId;
    }

    public static class ThisAttendance {
        /** 周几 */
        public String week;
        /** 是否考勤 */
        public boolean isClock;
        /** 是否假期 */
        public boolean isHoliday;
        /** 假期描述 */
        public String description;

    }

    /** 日期转化 */
    private String getWeek(int i) {
        switch (i) {
        case 1: {
            return "星期一(" + DateUtils.formatDate5(DateUtils.getThisWeek(new Date(), 1)) + ")";
        }
        case 2: {
            return "星期二(" + DateUtils.formatDate5(DateUtils.getThisWeek(new Date(), 2)) + ")";
        }
        case 3: {
            return "星期三(" + DateUtils.formatDate5(DateUtils.getThisWeek(new Date(), 3)) + ")";
        }
        case 4: {
            return "星期四(" + DateUtils.formatDate5(DateUtils.getThisWeek(new Date(), 4)) + ")";
        }
        case 5: {
            return "星期五(" + DateUtils.formatDate5(DateUtils.getThisWeek(new Date(), 5)) + ")";
        }
        case 6: {
            return "星期六(" + DateUtils.formatDate5(DateUtils.getThisWeek(new Date(), 6)) + ")";
        }
        case 7: {
            return "星期日(" + DateUtils.formatDate5(DateUtils.getThisWeek(new Date(), 7)) + ")";
        }
        default:
            return "-----";
        }
    }
}
